package com.chuantou.yuba.ui.widget;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chuantou.yuba.R;
import com.chuantou.yuba.ui.PublishNearFishingActivity;
import com.chuantou.yuba.ui.fragment.PublishFishingPlaceFragment;
import com.chuantou.yuba.ui.fragment.PublishJingXiaoShangFragment;
import com.chuantou.yuba.ui.fragment.PublishPinPaiShangFragment;
import com.chuantou.yuba.ui.fragment.PublishWangDianFragment;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.UiUtil;

public class UploadNearPopup extends PopupWindow implements View.OnClickListener {

	private View mContentView;
	private TextView mFishingPlaceView;
	private TextView mJingxiaoshangView;
	private TextView mPinpaishangView;
	private TextView mWangdianView;
	private Activity mActivity;

	public UploadNearPopup(Activity activity) {
		super(activity);
		this.mActivity = activity;
		mContentView = View.inflate(activity, R.layout.upload_near_popup, null);
		mFishingPlaceView = (TextView) mContentView.findViewById(R.id.fishing_place);
		mFishingPlaceView.setOnClickListener(this);
		mJingxiaoshangView = (TextView) mContentView.findViewById(R.id.jingxiaoshang);
		mJingxiaoshangView.setOnClickListener(this);
		mPinpaishangView = (TextView) mContentView.findViewById(R.id.pinpaishang);
		mPinpaishangView.setOnClickListener(this);
		mWangdianView = (TextView) mContentView.findViewById(R.id.wangdian);
		mWangdianView.setOnClickListener(this);
		mContentView.setOnClickListener(this);
		mContentView.findViewById(R.id.cancel).setOnClickListener(this);
		setContentView(mContentView);
		setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
		setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
		setFocusable(true);
		setAnimationStyle(R.style.popup_style);
		ColorDrawable drawable = new ColorDrawable(activity.getResources().getColor(R.color.translucence));
		setBackgroundDrawable(drawable);
	}

	public void dismissPopup() {
		Animation animation = AnimationUtils.loadAnimation(getContentView().getContext(), R.anim.to_bottom);
		mContentView.clearAnimation();
		animation.setFillAfter(true);
		animation.setAnimationListener(new DismissAnimationListener() {

			@Override
			public void onAnimationEnd(Animation animation) {
				super.onAnimationEnd(animation);
				new Handler().post(new Runnable() {

					@Override
					public void run() {
						UploadNearPopup.super.dismiss();
					}
				});
			}
		});
		mContentView.setAnimation(animation);
	}

	@Override
	public void dismiss() {
		dismissPopup();
	}

	@Override
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		super.showAsDropDown(anchor, xoff, yoff);
		startShowAnimation();
	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		startShowAnimation();
	}

	private void startShowAnimation() {
		mContentView.clearAnimation();
		Animation animation = AnimationUtils.loadAnimation(getContentView().getContext(), R.anim.from_bottom);
		animation.setFillAfter(true);
		mContentView.setAnimation(animation);
	}

	class DismissAnimationListener implements Animation.AnimationListener {

		private int position = -1;

		public DismissAnimationListener() {
		}

		public DismissAnimationListener(int position) {
			this.position = position;
		}

		public int getPosition() {
			return position;
		}

		@Override
		public void onAnimationStart(Animation animation) {

		}

		@Override
		public void onAnimationEnd(Animation animation) {

		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}

	}

	@Override
	public void onClick(View v) {
		if (UiUtil.isFastDoubleClick()) {
			return;
		}
		dismiss();
		if (v == mFishingPlaceView) {
			Intent intent = new Intent(mActivity, PublishNearFishingActivity.class);
			intent.putExtra(Constants.EXTRA_TITLE, mActivity.getString(R.string.fishing_place));
			intent.putExtra(Constants.EXTRA_DATA, PublishFishingPlaceFragment.class.getName());
			mActivity.startActivity(intent);
		} else if (v == mJingxiaoshangView) {
			Intent intent = new Intent(mActivity, PublishNearFishingActivity.class);
			intent.putExtra(Constants.EXTRA_TITLE, mActivity.getString(R.string.jingxiaoshang));
			intent.putExtra(Constants.EXTRA_DATA, PublishJingXiaoShangFragment.class.getName());
			mActivity.startActivity(intent);
		} else if (v == mPinpaishangView) {
			Intent intent = new Intent(mActivity, PublishNearFishingActivity.class);
			intent.putExtra(Constants.EXTRA_TITLE, mActivity.getString(R.string.pinpaishang));
			intent.putExtra(Constants.EXTRA_DATA, PublishPinPaiShangFragment.class.getName());
			mActivity.startActivity(intent);
		} else if (v == mWangdianView) {
			Intent intent = new Intent(mActivity, PublishNearFishingActivity.class);
			intent.putExtra(Constants.EXTRA_TITLE, mActivity.getString(R.string.weiwang));
			intent.putExtra(Constants.EXTRA_DATA, PublishWangDianFragment.class.getName());
			mActivity.startActivity(intent);
		}
	}
}